package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.util.AtguigUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.io.IOException;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/18 9:19
 */
public class Flink08_State_Backend {
    public static void main(String[] args) throws IOException {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env.enableCheckpointing(1000);// checkpoint周期
        //1. 内存
        // 1.1 旧的写法
        //env.setStateBackend(new MemoryStateBackend());  // 默认
        // 1.2 新的写法
//        env.setStateBackend(new HashMapStateBackend()); // 本地适应内存
//        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());  // 设置checkpoint的存储
        
        // 2. fs
        // 2.1 旧的写法
//        env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/ck"));
        // 2.2 新的写法
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck1");
        
        // 3. rockdb
        // 3.1 旧的写法
//        env.setStateBackend(new RocksDBStateBackend("hdfs://hadoop162:8020/ck2"));
        // 3.2 新的写法
        env.setStateBackend(new EmbeddedRocksDBStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck3");
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(new MapFunction<String, WaterSensor>() {
                @Override
                public WaterSensor map(String line) throws Exception {
                    String[] data = line.split(",");
                    return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
                }
            })
            .keyBy(WaterSensor::getId)
            
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                
                private MapState<Integer, Object> vcMapState;
                
                @Override
                public void open(Configuration parameters) throws Exception {
                    vcMapState = getRuntimeContext()
                        .getMapState(
                            new MapStateDescriptor<Integer, Object>("vcMapState", Integer.class, Object.class));
                }
                
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    vcMapState.put(value.getVc(), new Object());
    
                    Iterable<Integer> keys = vcMapState.keys();
                    List<Integer> vcs = AtguigUtil.toList(keys);
                    out.collect(ctx.getCurrentKey() + " 的不重复的水位值: " + vcs);
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

